package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Exam;
import com.han.bom.ExamResponse;
import com.han.bom.PracticeExam;
import com.han.bom.Question;
import com.han.bom.RealExam;
import com.han.bom.Student;
import com.han.dao.concrete.product.AnswerDAO;
import com.han.dao.concrete.product.ExamDAO;
import com.han.dao.concrete.product.ExamResponseDAO;
import com.han.dao.concrete.product.QuestionDAO;

/**
 * Servlet implementation class PreViewExamsServlet
 */
@WebServlet("/Student/ViewExamsServlet")
public class ViewExamsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private Logger logger = Logger.getLogger(getClass().getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewExamsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		logger.info("Entered ViewExamsServlet GET");
		Student student = null;
		HttpSession session = request.getSession();
		ExamResponseDAO examresponsedao = new ExamResponseDAO();
		ExamDAO examdao = new ExamDAO();
		QuestionDAO questiondao = new QuestionDAO();
		AnswerDAO answerdao = new AnswerDAO();
		String type = null;

		if(session.getAttribute("student")!=null){
			student = (Student)session.getAttribute("student");
		}
		else{
			
		}
		
		if(request.getParameter("type")!=null){
			type = request.getParameter("type");
		}
		
		ArrayList<String> examIDsTaken = new ArrayList<String>();
		ArrayList<ExamResponse> examsMissed = new ArrayList<ExamResponse>();
		
		try {
			examIDsTaken = examresponsedao.retrieveExamsTaken(student.getUserID());
			examsMissed = examresponsedao.retrieveExamsMissed(student.getUserID());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		} catch (NamingException e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		
		ArrayList<ExamResponse> examresponses = new ArrayList<ExamResponse>();
		
		for(String examid:examIDsTaken){
			
			try {
				Exam exam = examdao.retrieveExamById(examid);
				
				ArrayList<Question> questions = questiondao.retrieveQuestionsbyExam(examid);
				
				for(Question question:questions){
					ArrayList<String> choices = answerdao.retrieveAnswersByQuestion(question.getQuestionID());
					question.setChoices(choices);
					question.setAnswer(choices.get(0));
				}
				
				if(exam instanceof PracticeExam){
					((PracticeExam) exam).setTimesTaken(examresponsedao.retrieveResponseCountByStudentAndExam(student.getUserID(), exam.getExamID()));
				}
				
				exam.setQuestions(questions);
				
				examresponses.addAll(examresponsedao.retrieveResponsesByStudentAndExam(student.getUserID(), exam));
			
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (NamingException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
		}
	
		RequestDispatcher rd = null;
		
		if("Real".equals(type)){
			
			for(ExamResponse er:examsMissed){
				Exam exam = er.getExam();
				try {
					exam.setQuestions(questiondao.retrieveQuestionsbyExam(exam.getExamID()));
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				} catch (NamingException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				}
			}
			request.setAttribute("missed", examsMissed);
			
			for(int i=0;i<examresponses.size();i++){
				if(examresponses.get(i).getExam() instanceof PracticeExam){
					examresponses.remove(i);
					i--;
				}
			}
			
			rd = request.getRequestDispatcher("Exam_Real.jsp");
		}
		else{
			
			for(int i=0;i<examresponses.size();i++){
				if(examresponses.get(i).getExam() instanceof RealExam){
					examresponses.remove(i);
					i--;
				}
			}
			
			rd = request.getRequestDispatcher("Exam_Practice.jsp");
		}
		
		
		
		request.setAttribute("examresponses", examresponses);
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
